package com.gexin.controller.system.product;

import com.gexin.core.web.SystemBaseController;
import com.gexin.model.product.ProductType;
import com.gexin.model.shop.ShopInfo;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 商品类型管理
 */
@ControllerBind(controllerKey = "/system/product/types",viewPath = "WEB-INF/system/product")
public class CommodityTypesController extends SystemBaseController {

    public void index(){
        ProductType types = getModel(ProductType.class,"types");
        setAttr("types",types);
        Page<ProductType> data = ProductType.dao.paginate(getpage(), getpageSize(),types, ShopInfo.shop_id);
        for(ProductType a : data.getList()){
            if(!StringUtils.isEmpty(a.getShowType())){
                String [] show_types=a.getShowType().split(",");
                List<String>name=new LinkedList<>();
                for(int i=0;i<show_types.length;i++){
                    String type_names="1".equals(show_types[i])?"在售商品":"敬请期待";
                    name.add(type_names);
                }
                a.put("show_type_name",StringUtils.join(name,"，"));
                a.put("show_type_list",show_types);
            }else{
                a.put("show_type_name","无");
                a.put("show_type_list",new ArrayList<>());
            }

            List<ProductType> list = ProductType.dao.findByPid(a.getId());
            a.put("list1",list);
        }
        paginate(data);
        render("commodity_types_list.html");
    }

    public void save(){
        ProductType types = getModel(ProductType.class,"types");
        List<Integer> type_list=new LinkedList<>();
        Integer show_type_1=getParaToInt("show_type_1");
        Integer show_type_2=getParaToInt("show_type_2");
        Integer showTypeIntegral = getParaToInt("show_type_integral");
        if(show_type_1!=null){
            type_list.add(show_type_1);
        }
        if(show_type_2!=null){
            type_list.add(show_type_2);
        }
        if(null!=showTypeIntegral){
            type_list.add(showTypeIntegral);
        }
        types.setShowType(StringUtils.join(type_list,","));
        if(types.getId()==null){
            if(types.getPid()==null){
                types.setPid(0);
            }
            types.setShopId(ShopInfo.shop_id);
            types.save();
        }else {
            types.update();
        }
        success();
    }

    //状态
    public void updState(){
        Integer id = getParaToInt();
        ProductType types = ProductType.dao.findById(id);
        if(ProductType.dao.finByState(id)!=null){
            fail("有关联二级菜单");
            return;
        }
        types.setState(getParaToInt("state"));
        types.update();
        success();
    }

    //页面弹窗
    public void info(){
        ProductType types = ProductType.dao.findById(getParaToInt("id"));
        List<Integer> show_list=new ArrayList<>();
        if(!StringUtils.isEmpty(types.getShowType())){
           String [] shows= types.getShowType().split(",");
           for(int i=0;i<shows.length;i++){
               show_list.add(Integer.valueOf(shows[i]));
           }
        }
        types.put("show_list",show_list);

        renderJson(types);
    }

    //删除 假删除
    public void delType(){
        ProductType types = ProductType.dao.findById(getParaToInt("id"));
        if(types!=null){
            if(ProductType.dao.finByState(types.getId())!=null){
                fail("请先删除二级菜单");
                return;
            }
            types.setDelState(1);
            types.update();
            success("删除成功");
        }else{
            success("删除失败，未找到此类型");
        }
    }


}
